package com.pengxun.manager.controller.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.pengxun.manager.model.system.Menu;
import com.pengxun.manager.model.system.Permission;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.PermissionService;
import com.pengxun.manager.vo.PermissionVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/permission")
public class  PermissionController {

   @Resource
   private PermissionService permissionService;
   
   @Resource
   private LoginService loginService;
   
   @RequestMapping("/insert")
   public Result insert(@Valid @RequestBody Permission permission,BindingResult result){
       try{
    	   if(result.hasErrors()) {
    		   throw new EastrocException(result.getFieldError().getDefaultMessage());
    	   }
    	   Userinfo user = loginService.getCurrentUser();
    	   permission.setCreateBy(user.getUserId());
    	   permission.setUpdateBy(user.getUserId());
    	   Date date = new Date();
    	   permission.setCreateDate(date);
    	   permission.setUpdateDate(date);
          permissionService.insert(permission);
       } catch (Exception e) {
    	   e.printStackTrace();
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   }
   
   @PostMapping("/update")
   public Result update(@Valid @RequestBody Permission permission,BindingResult result){
        try{
			if(result.hasErrors()) {
			    throw new EastrocException(result.getFieldError().getDefaultMessage());
		    }
			Userinfo user = loginService.getCurrentUser();
			permission.setUpdateBy(user.getUserId());
			Date date = new Date();
			permission.setUpdateDate(date);
		  permissionService.update(permission);
       } catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   }
   
   @RequestMapping("/delete")
   public Result delete(Integer permissionId){
       try{
          permissionService.delete(permissionId);
       } catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   }
   
   @RequestMapping("/findByPage")
   public Result findByPage(@RequestBody PermissionVO permission) {
		Page page = permissionService.findByPage(permission);
		return ResultUtils.render(page);
	}
	
   @RequestMapping("/findByPermissionId")
   public Result findById(Integer  permissionId) {
		Permission permission = permissionService.findByPermissionId(permissionId);
		return ResultUtils.render(permission);
	}
   
   @RequestMapping("/updateStatus")
   public Result updateStatus(Integer permissionId,Integer status){
	   try{
		   permissionService.updateStatus(permissionId,status);
	   } catch (Exception e) {
		   return ResultUtils.renderError(e.getMessage());
	   }
	   return ResultUtils.render();
   }

	@PostMapping("/syncPermissions")
	public Result syncPermissions(@RequestBody List<Menu> list) {
		log.info("list==={}", JSON.toJSONString(list));
//	   menuService.deleteAll();
		permissionService.deleteAll();
		permissionService.syncPermissions(list);
		return ResultUtils.render();
	}
}
